package com.lin.mr.maizilaundry.fragmentorder;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.myadapter.OrderAdapter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentOrderHome extends Fragment {
    private List<Fragment> fragmentList;
    private ImageView iv_back;
    private ImageView iv_camera;
    private ViewPager vp_order;
    private PagerSlidingTabStrip pagerSlidingTabStrip;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_home, container, false);
        this.view = view;
        initView();
        return view;
    }

    private void initView() {
        fragmentList = new ArrayList<>();
        iv_back = (ImageView) view.findViewById(R.id.iv_back_order);
        iv_camera = (ImageView) view.findViewById(R.id.iv_camera);
        vp_order = (ViewPager) view.findViewById(R.id.vp_order);
        pagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.pagerSlide);

        FragmentUnpay unPay = new FragmentUnpay();
        FragmentPayed payed = new FragmentPayed();
        FragmentCompleted completed = new FragmentCompleted();
        fragmentList.add(unPay);
        fragmentList.add(payed);
        fragmentList.add(completed);
        vp_order.setAdapter(new OrderAdapter(getActivity().getSupportFragmentManager(),fragmentList));
        pagerSlidingTabStrip.setViewPager(vp_order);//将要呈现的vp_order传给pagerSlidingTabStrip

        //返回按钮的点击事件
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        //照相机的点击事件
        iv_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//启动系统摄像头
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Activity.RESULT_OK){
            String sdState = Environment.getExternalStorageState();
            if (!sdState.equals(Environment.MEDIA_MOUNTED)){ //检测SD卡是否可用
                Toast.makeText(getActivity(), "SD卡不可用!", Toast.LENGTH_SHORT).show();
                return;
            }
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
            FileOutputStream outputStream = null;
            //创建存放照片的文件夹路径
            String path = Environment.getExternalStorageDirectory().toString()+"/MaiZiPhoto";
            //创建存放照片的文件夹
            File file = new File(path);
            if (!file.exists()){ //判断文件夹是否存在
                file.mkdirs();
            }
            //保存的照片名称和格式
            String photoName = System.currentTimeMillis()+".jpg";

            try {
                outputStream = new FileOutputStream(path+"/"+photoName);
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (requestCode == 1){
            Toast.makeText(getActivity(), "照片已保存至MaiZiPhoto", Toast.LENGTH_SHORT).show();
        }
    }
}
